﻿@page "/mainplan/add"

@inject IJSRuntime JsRuntime
@inject NavigationManager NavigationManager
@inject IMainPlanService MainPlanService

<PageTitle>新增主计划</PageTitle>

<EditForm Model="_mainPlanDto" OnSubmit="Save">
    <div class="row">
        <div class="col input-group mb-2">
            <span class="input-group-text">创建时间</span>
            <input type="date" class="form-control" @bind-value="@_mainPlanDto.CreateTime" />
        </div>
        
        <div class="col input-group mb-2">
            <span class="input-group-text">项目月份</span>
            <input type="month" class="form-control" @bind-value="@_mainPlanDto.MonthOfInvoice" />
        </div>
        <div class="col"></div>
        <div class="col"></div>
    </div>
    <div class="row">
        <div class="col input-group mb-2">
            <span class="input-group-text">完工时间</span>
            <input type="date" class="form-control" @bind-value="@_mainPlanDto.FinishTime" />
        </div>
        <div class="col input-group mb-2">
            <span class="input-group-text">计划发图</span>
            <input type="date" class="form-control" @bind-value="@_mainPlanDto.DrwReleaseTarget" />
        </div>
        <div class="col"></div>
        <div class="col"></div>
    </div>
    <div class="row">
        <div class="col input-group mb-2">
            <span class="input-group-text">编号</span>
            <input class="form-control" placeholder="编号" @bind-value="@_mainPlanDto.Number" />
        </div>
        <div class="col input-group mb-2">
            <span class="input-group-text">分批</span>
            <input class="form-control" type="number" @bind-value="@_mainPlanDto.Batch" />
        </div>
        <div class="col"></div>
        <div class="col"></div>
    </div>
    
    <div class="input-group mb-2">
        <span class="input-group-text">项目名称</span>
        <input class="form-control" placeholder="项目名称" @bind-value="@_mainPlanDto.Name" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">烟罩数量</span>
        <input class="form-control" type="number" @bind-value="@_mainPlanDto.Quantity" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">烟罩型号</span>
        <input class="form-control" placeholder="型号" @bind-value="@_mainPlanDto.ModelSummary" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">项目类型</span>
        <select class="form-select" @bind="@_mainPlanDto.MainPlanType">
            <CompEnumList Type="typeof(MainPlanType_e)" />
        </select>
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">项目备注</span>
        <textarea class="form-control" placeholder="备注，可输入多行" style="height: 120px" @bind="@_mainPlanDto.Remarks"></textarea>
    </div>

    <CompSaveCancelButtons CancelUrl="mainplans" />
</EditForm>
@code {

    private MainPlanDto _mainPlanDto = new() { CreateTime =DateTime.Now, FinishTime = DateTime.Now.AddDays(14), DrwReleaseTarget=DateTime.Now.AddDays(7), MonthOfInvoice=DateTime.Now ,MainPlanType = MainPlanType_e.ETO,Batch = 0};
    
    private async Task Save()
    {
        if (string.IsNullOrWhiteSpace(_mainPlanDto.Number)||string.IsNullOrWhiteSpace(_mainPlanDto.Name))
        {
            await JsRuntime.InvokeVoidAsync("alert", "填写的信息不完整");
            return;
        }
        //为了避免FSO号前后有空格，导致后续创建文件夹时出现错误，此处消除空格
        _mainPlanDto.Number = _mainPlanDto.Number.Trim().ToUpper();

        var same = await MainPlanService.GetFirstOrDefaultByNumberAsync(_mainPlanDto.Number);
        if (same.Status)
        {
            await JsRuntime.InvokeVoidAsync("alert", "项目编号重复，请重新修改项目编号！");
            return;
        }
        var result = await MainPlanService.AddAsync(_mainPlanDto);
        //跳转回主页
        if (result.IsSuccessStatusCode) NavigationManager.NavigateTo("mainplans");
    }
}
